package com.ymatou.iapi.mqcompensate.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.mqcompensate.parameter.CheckToChompensateBean;
import com.ymatou.iapi.mqcompensate.service.CheckToChompensateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.mongo.MQMessageCompensateBean;
import com.ymttest.database.mongo.MQMessageCompensateWapper;
import com.ymttest.database.mongo.MQMessageWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*检查需要补单的消息入库用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckToChompensate{
	private static CheckToChompensateBean checktochompensateBean;
	private static CheckToChompensateCall checktochompensateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("检查需要补单的消息入库用例");
	}

	@Before
	public void caseUp() {
		checktochompensateBean=new CheckToChompensateBean();
		checktochompensateCall=new CheckToChompensateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checktochompensateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckToChompensate_001() {
		Logger.start(true,"检查需要补单的消息入库");
		try {
			
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "qusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 9*60 * 60 * 1000);
			
			Date date2 = new Date(nowTime.getTime() - 8*60 * 60 * 1000+20*60*1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(date);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);
			
			checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("null", checktochompensateCall.getMsg(), "验证Msg");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName, uuid);
				
			Logger.verifyEquals(appName, compensate.getAppid(), "验证数据库MQ_Message_Compensate字段AppId");
			Logger.verifyEquals(codeName, compensate.getCode(), "验证数据库MQ_Message_Compensate字段Code");
			Logger.verifyEquals(msguniqueid, compensate.getMid(), "验证数据库MQ_Message_Compensate字段mId");
//			Logger.verifyEquals(format.format(date2).subSequence(0, 15), compensate.getRtimeout().toString().subSequence(7, 22), "验证数据库MQ_Message_Compensate字段Rtimeout");
			Logger.verifyEquals(0, compensate.getRetrycount(), "验证数据库MQ_Message_Compensate字段RetryCount");
			Logger.verifyEquals(3, compensate.getSource(), "验证数据库MQ_Message_Compensate字段Source补单来源:3-补偿站");
			Logger.verifyEquals(0, compensate.getNstatus(), "验证数据库MQ_Message_Compensate字段nStatus");
			Logger.verifyEquals("bamei_qusi_c1", compensate.getCid(), "验证数据库MQ_Message_Compensate字段Cid");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckToChompensate_002() {
		Logger.start(true,"检查nstatus!=0的消息不会入库");
		try {
			
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "qusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8*60 * 60 * 1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(date);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 1,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);
			
			checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("null", checktochompensateCall.getMsg(), "验证Msg");
			
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName, uuid);
			
			Logger.verifyIsNull(compensate, "验证未进数据库MQ_Message_Compensate");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckToChompensate_003() {
		Logger.start(true,"检查Pstatus!=0的消息不会入库");
		try {
			
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "qusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 20 * 60 * 1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(date);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 1," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);
			
			checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("null", checktochompensateCall.getMsg(), "验证Msg");
			
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName, uuid);
			
			Logger.verifyIsNull(compensate, "验证未进数据库MQ_Message_Compensate");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckToChompensate_004() {
		Logger.start(true,"检查ctime大于5分钟前的消息不会入库-调整检测进入补单的时间为10分钟-JIRACONN-3728");
		try {
			
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "qusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 9*60 * 60 * 1000);
			
			Date date2 = new Date(nowTime.getTime() - 8*60 * 60 * 1000+8*60*1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(date2);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);			
			
			checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("null", checktochompensateCall.getMsg(), "验证Msg");

			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName, uuid);
			
			Logger.verifyIsNull(compensate, "验证未进数据库MQ_Message_Compensate");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckToChompensate_005() {
		Logger.start(false,"检查需要补单的消息入库-AppId空");
		try {

			String appName = "testbamei";
			String codeName = "qusi";

			//checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals("400", checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid appId:null", checktochompensateCall.getMsg(), "验证Msg");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckToChompensate_006() {
		Logger.start(false,"检查需要补单的消息入库-Code空");
		try {

			String appName = "testbamei";
			String codeName = "qusi";

			checktochompensateBean.setAppId(appName);
			//checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals("400", checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid code:null", checktochompensateCall.getMsg(), "验证Msg");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckToChompensate_007() {
		Logger.start(false,"检查需要补单的消息入库-AppId不存在");
		try {

			String appName = "testge";
			String codeName = "qusi";

			//checktochompensateBean.setAppId(appName);
			checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals("400", checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid appId:null", checktochompensateCall.getMsg(), "验证Msg");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckToChompensate_008() {
		Logger.start(false,"检查需要补单的消息入库-Code不存在");
		try {

			String appName = "testbamei";
			String codeName = "buqusi";

			checktochompensateBean.setAppId(appName);
			//checktochompensateBean.setCode(codeName);
			
			checktochompensateCall.setData(checktochompensateBean);
			checktochompensateCall.callService();			
			
			Logger.verifyEquals("400", checktochompensateCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid code:null", checktochompensateCall.getMsg(), "验证Msg");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}